import tkinter as tk
from tkinter import ttk
import json
import os
from config_manager import ConfigManager  # 导入 ConfigManager

class SettingFrame:
    def __init__(self, settings_frame, language_switcher=None, config_manager=None):
        self.settings_frame = settings_frame
        self.language_switcher = language_switcher
        self.config_manager = config_manager  # 接收 ConfigManager 实例
        self.config = self.config_manager.get_config()  # 获取配置

        # 创建所有Entry控件
        self.create_widgets()

        # 加载初始状态
        self.load_initial_state()

        if self.language_switcher:
            self.language_switcher.lang_manager.register_callback(self.update_texts)

        # 添加保存按钮
        self.save_btn = ttk.Button(self.settings_frame, text=self.language_switcher.lang_manager.get_text("setting_page", "save_btn"), command=self.save_setting_config)
        self.save_btn.grid(row=8, column=0, columnspan=4, pady=20, sticky='n')

        # 配置父容器的行权重，使按钮保持在底部
        self.settings_frame.rowconfigure(8, weight=1)
        self.settings_frame.columnconfigure(0, weight=1)
        self.settings_frame.columnconfigure(1, weight=1)
        self.settings_frame.columnconfigure(2, weight=1)
        self.settings_frame.columnconfigure(3, weight=1)
        # 配置样式
        self.configure_styles()

    def configure_styles(self):
        style = ttk.Style()
        style.configure("TLabelFrame", font=('Arial', 12, 'bold'))
        style.configure("TLabel", font=('Arial', 12))
        style.configure("TEntry", font=('Arial', 12))
        style.configure("TCheckbutton", font=('Arial', 12))
        style.configure("TButton", font=('Arial', 14, 'bold'))

    def update_texts(self):
        # 更新所有控件的文本
        self.save_btn.config(text=self.language_switcher.lang_manager.get_text("setting_page", "save_btn"))
        for key, config in self.config.items():
            frame = getattr(self, f"{key}_frame")
            frame.config(text=self.language_switcher.lang_manager.get_text("setting_page", f"{key}_check"))
            label = getattr(self, f"{key}_label")
            label.config(text=self.language_switcher.lang_manager.get_text("setting_page", f"{key}_label"))

    def create_widgets(self):
        # 动态创建控件
        for i, (key, config) in enumerate(self.config.items()):
            row = i // 3  # 每行3个控件
            col = i % 3

            # 创建LabelFrame
            frame = ttk.LabelFrame(self.settings_frame, text=self.language_switcher.lang_manager.get_text("setting_page", f"{key}_check"))
            frame.grid(row=row, column=col, padx=10, pady=10, sticky='ew')
            setattr(self, f"{key}_frame", frame)

            # 创建Label
            label = ttk.Label(frame, text=self.language_switcher.lang_manager.get_text("setting_page", f"{key}_label"))
            label.grid(row=0, column=1)
            setattr(self, f"{key}_label", label)

            # 创建Entry
            entry = ttk.Entry(frame, width=40)
            entry.grid(row=0, column=2)
            setattr(self, f"{key}_entry", entry)

            # 创建Checkbutton
            check_var = tk.BooleanVar()
            setattr(self, f"{key}_check_var", check_var)
            check = ttk.Checkbutton(frame, variable=check_var,
                                    command=lambda var=check_var, lbl=label, ent=entry: self.toggle_frame_state(var, lbl, ent))
            check.grid(row=0, column=0, padx=5, pady=5, sticky='w')

    def save_setting_config(self):
        # 更新config中的值
        for key in self.config:
            check_var = getattr(self, f"{key}_check_var")
            entry = getattr(self, f"{key}_entry")
            self.config_manager.update_config(key, check_var.get(), entry.get())  # 使用 ConfigManager 更新配置

    def load_initial_state(self):
        for key, config in self.config.items():
            check_var = getattr(self, f"{key}_check_var")
            label = getattr(self, f"{key}_label")
            entry = getattr(self, f"{key}_entry")

            check_var.set(config["enabled"])
            entry.insert(0, config["value"])
            self.toggle_frame_state(check_var, label, entry)

    def toggle_frame_state(self, check_var, label, entry):
        if check_var.get():
            label.config(state='normal')
            entry.config(state='normal')
        else:
            label.config(state='disabled')
            entry.config(state='disabled')


 